package com.example.selenium.core;

import com.example.selenium.AutomaticGrab;
import com.example.selenium.core.other.WaybillU;
import com.example.selenium.util.HttpClientUtil;
import com.example.selenium.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class SaticScheduleTask {

    @Autowired
    private AutomaticGrab automaticGrab;

    /**
     * 3.添加定时任务,每天凌晨2点钟，进行更新前三天的运单状态
     */
    @Scheduled(cron = "0 0 1 * * ?")
    private void configureTasks() {
        System.err.println("定时任务---->自动更新运单状态，当前时间[" + LocalDateTime.now()+"]");
        // 先调用接口，测试cookie是否有效
        try {
            automaticGrab.testCookieOnline();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 更新前三天内的运单
        String startTime = LocalDate.now().minusDays(3).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))+" 00:00:00";
        String endTime = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))+" 00:00:00";
        System.err.println("自动更新3天内的运单状态，开始时间："+startTime+",结束时间："+endTime);
        try {
            automaticGrab.uploadWaybillStatusIn3Days(startTime,endTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }


}